<?php
namespace Home\Controller;
use Think\Controller;
class GoodController extends Controller {
	//商品预定页面
    public function index()
    {
    	$id = I('get.id');
        
        $good = M('goods')->where(array('goods_id'=>$id))->find();
        $this->assign('good',$good);

        //取comm
		$count      = M('comm')->where(array('goods_id'=>$id,'is_shenhe'=>1))->count();// 查询满足要求的总记录数
		$this->assign('count',$count);// 总条数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = M('comm')->where(array('goods_id'=>$id,'is_shenhe'=>1))->order('commtime')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //提交预定数据
    public function add()
    {//Array ( [name] => x [phone] => xx [yzm] => x [num] => 4 [price] => 3999.00 [goods_id] => 3 [cat_id] => 2 [goods_name] => 1003男士西服 )
    	if (IS_POST) {
    		$data['name'] = I('post.name');

    		$data['phone'] = I('post.phone');
    		$send_to = I('post.yzm');

    		$data['num'] = I('post.num');
    		$price = I('post.price') * $data['num'];
    		$data['price'] = $price;

    		$data['goods_id'] = I('post.goods_id');
    		$data['cat_id'] = I('post.cat_id');
    		$data['goods_name'] = I('post.goods_name');

    		$data['addtime'] = time();

            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }

	        //短信随机码验证
	        if ($send_to != $_SESSION['sj_mobile']) {
				$arr = array(
					'error' => 0,
					'info' => '验证码错误'
					);
				echo json_encode($arr);
				exit;
	        }

	        //入库
	        $res = M('reserve')->add($data);
	        if ($res) {
				$arr = array(
					'error' => 1,
					'info' => '预定成功,请保持手机畅通'
					);
				echo json_encode($arr);
	        }else {
				$arr = array(
					'error' => 0,
					'info' => '预定失败,麻烦重新提交信息'
					);
				echo json_encode($arr);
	        }

    	}
    }

	//秒滴短信发送开始----------------------
    public function send_to()
    {
    	if (IS_POST) {
    		$to = I('post.to');

	       	 //手机
	       	 $preg_js = "/^1[34578][0-9]{9}$/";
	       	 $pass_sj = preg_match($preg_js, $to);
			 if (!$pass_sj) {
				$arr = array(
					'error' => 0,
					'info' => '请输入正确的手机号'
					);
				echo json_encode($arr);
				exit;
			 }

			 //发送
			miaodi($to);
			$arr = array(
				'error' => 1,
				'info' => '正在发送...',
				);
			echo json_encode($arr);
			 
    	}
    }


    //评论
    public function comm()
    {//Array ( [nick] => [ph_em] => [content] => xx [goods_id] => 4 [cat_id] => 2 [goods_name] => 1004男士西服 )
    	if (IS_POST) {
    		$data['nick'] = I('post.nick')?I('post.nick'):'匿名';
    		$data['ph_em'] = I('post.ph_em')?I('post.ph_em'):'无';
    		$data['content'] = I('post.content');

    		$data['goods_id'] = I('post.goods_id');
    		$data['cat_id'] = I('post.cat_id');
    		$data['goods_name'] = I('post.goods_name');

    		$data['commtime'] = time();

    		//内容不空
    		if (empty($data['content'])) {
				$arr = array(
					'error' => 0,
					'info' => '请输入评论内容'
					);
				echo json_encode($arr);
				exit;
    		}

    		//入库
    		$res = M('comm')->add($data);
	        if ($res) {
	        	$url = U('home/good/index',array('id'=>$data['goods_id']));
				$arr = array(
					'error' => 1,
					'info' => '评论成功',
					'url' => $url
					);
				echo json_encode($arr);
	        }else {
				$arr = array(
					'error' => 0,
					'info' => '评论失败'
					);
				echo json_encode($arr);
	        }
    	}
    }

    //点赞
    public function dianzan()
    {
        if (IS_POST) {
            $zan = I('post.zan');
            $row = M('comm')->where(array('id'=>$zan))->setInc('zan'); // zan字段加1
            if ($row) {
                $zan = M('comm')->where(array('id'=>$zan))->find();
                $arr = array(
                'error' => 1,
                'info' => '点赞成功',
                'zan' => $zan['zan']
                );
                echo json_encode($arr);
            }else {
                $arr = array(
                'error' => 0,
                'info' => '点赞失败'
                );
                echo json_encode($arr);
            }
        }
    }



}